package cn.signalpha.modules.shop.controller.api;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.support.Kv;
import cn.signalpha.core.tool.utils.BeanUtil;
import cn.signalpha.core.tool.utils.CollectionUtil;
import cn.signalpha.modules.shop.entity.Publicity;
import cn.signalpha.modules.shop.service.PublicityGoodsService;
import cn.signalpha.modules.shop.service.PublicityService;
import cn.signalpha.modules.shop.vo.PublicityGoodsVO;
import cn.signalpha.modules.shop.vo.PublicityVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/wap/publicity")
@Api(value = "营销推广", tags = "营销推广")
public class ApiPublicityController extends BaseController {

	private final PublicityService publicityService;
	private final PublicityGoodsService publicityGoodsService;

	@GetMapping("/list")
	@ApiOperation(value = "列表", notes = "")
	public R<List<Publicity>> list(@ApiParam(value = "类型", required = true) @RequestParam String type) {
		return R.data(publicityService.listByType(type));
	}

	@GetMapping("/model")
	@ApiOperation(value = "首页模块", notes = "")
	public R<PublicityVO> model(@ApiParam(value = "类型", required = true) @RequestParam String type) {
		List<Publicity> publicityList = publicityService.listByType(type);
		if(CollectionUtil.isEmpty(publicityList)){
			return R.data(null);
		}

		PublicityVO publicityVO = Objects.requireNonNull(BeanUtil.copy(publicityList.get(0), PublicityVO.class));
		IPage<PublicityGoodsVO> page = publicityGoodsService.selectPublicityGoodsPage(
			Condition.getPage(new Query().setSize(2)),
			Kv.create().set("publicityId", publicityVO.getId().toString()).set("goodsStatus", "1")
		);
		publicityVO.setPublicityGoodsList(page.getRecords());
		return R.data(publicityVO);
	}

	@GetMapping("/info")
	@ApiOperation(value = "详情", notes = "传入id")
	public R<Publicity> info(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
		return R.data(publicityService.getById(id));
	}

	@GetMapping("/goods/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name="publicityId", dataType = "string", value = "营销推广id"),
		@ApiImplicitParam(name="goodsTitle", dataType = "string", value = "商品标题"),
	})
	@ApiOperation(value = "商品列表", notes = "")
	public R<IPage<PublicityGoodsVO>> goodsPage(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		params.put("goodsStatus", "1");
		return R.data(publicityGoodsService.selectPublicityGoodsPage(Condition.getPage(query), params));
	}

}
